<?php

/**
 * 创建日志
 */

declare(strict_types=1);

namespace app\admin\listener;

use think\Request;
use app\admin\traits\AdminAuth;

use think\facade\Log;
use app\admin\exception\AdminServiceException;
use app\admin\model\AdminMenu;

class SystemLog
{
    use AdminAuth;

    public function handle($user, Request $request): void
    {
        try {

            //获取当前访问url
            $url = parse_name(app('http')->getName()) . '/' .
                parse_name($request->controller()) . '/' .
                parse_name($request->action());
            //记录日志
            $menu = AdminMenu::where(['url' => $url])->findOrEmpty();

            if (isset($user) && !$menu->isEmpty() && $request->method() === $menu->log_method) {
                $this->createAdminLog($user, $menu->name);
            }
            // 记录日志
        } catch (AdminServiceException $e) {
            Log::error('记录登录异常，信息：' . $e->getMessage());
        }
    }
}
